package someTestExcemple.huaweiOd.container.tree;

//https://blog.csdn.net/guorui_java/article/details/132524176
//华为OD机试 - 最少数量线段覆盖 - 二叉树（Java 2025 A卷 100分）
import java.util.*;

class Segment {
    int start;
    int end;

    public Segment(int start, int end) {
        this.start = start;
        this.end = end;
    }
}

public class LineCovered {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 读取线段的数量
        int n = scanner.nextInt();
        scanner.nextLine();

        Segment[] segments = new Segment[n];
        for (int i = 0; i < n; i++) {
            String[] input = scanner.nextLine().split(",");
            int start = Integer.parseInt(input[0]);
            int end = Integer.parseInt(input[1]);
            segments[i] = new Segment(start, end);
        }
        scanner.close();

        int result = findMinimumSegments(segments);
        System.out.println(result);
    }

    public static int findMinimumSegments(Segment[] segments) {
        // 按起点排序，如果起点相同则按终点排序
        //todo这种写法学习一下
        Arrays.sort(segments, Comparator.comparingInt((Segment s) -> s.start).thenComparingInt(s -> s.end));

        int count = 1;
        int currentEnd = segments[0].end;
        int currentStart = segments[0].start;
        for(int i=1;i<segments.length;i++) {
            if(segments[i].end > currentEnd) {
                count++;
                currentEnd =  segments[i].end;
            }
        }

//        while (index < segments.length) {
//            int maxEnd = currentEnd;
//            // 找到能覆盖当前未覆盖区域且终点最远的线段
//            while (index < segments.length && segments[index].start <= currentEnd) {
//                maxEnd = Math.max(maxEnd, segments[index].end);
//                index++;
//            }
//            // 如果没有找到合适的线段，说明无法完全覆盖
//            if (maxEnd == currentEnd) {
//                if (index < segments.length) {
//                    if (currentEnd == Integer.MIN_VALUE) {
//                        currentEnd = segments[index].end;
//                        count++;
//                        index++;
//                    } else {
//                        return -1;
//                    }
//                } else {
//                    break;
//                }
//            } else {
//                currentEnd = maxEnd;
//                count++;
//            }
//        }

        return count;
    }
}
